package com.controller;

import com.domain.Blog;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.BlogService;
import com.util.PageUtil;
import com.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
public class IndexController {
    @Resource
    private BlogService blogService;
    @RequestMapping({"/index","/"})
    public ModelAndView index(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "typeId", required = false) String typeId, @RequestParam(value = "releaseDateStr", required = false) String releaseDateStr, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("title", "大众博客");
        if (StringUtil.isEmpty(page)) {
            page = "1";
        }
        PageHelper.startPage(Integer.parseInt(page),10);
        HashMap<String, Object> map = new HashMap<>();
        map.put("typeId", typeId);
        map.put("releaseDateStr", releaseDateStr);
        List<Blog> list = blogService.list(map);
        PageInfo<Blog> pageInfo=new PageInfo<Blog>(list);
        StringBuffer para = new StringBuffer();
        if (StringUtil.isNotEmpty(typeId)) {
            para.append("typeId=" + typeId + "&");
        }
        if (StringUtil.isNotEmpty((releaseDateStr))) {
            para.append("releaseDateStr" + releaseDateStr + "&");
        }
        mv.addObject("mainPage", "foreground/blog/list.jsp");
        mv.addObject("pageInfo", pageInfo);
        return mv;
    }
}
